package com.cs;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName: NIOFileChannel01
 * @Description: FileOutputStream 包含了 Channel
 * @Author: kevinHan
 * @Date: 2020/8/6 21:03
 * @Version: 1.0
 */
public class NIOFileChannel01 {
    public static void main(String[] args) throws Exception {
        String str = "hello,万俟凯";
        // 创建一个输出流 -> channel
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");

        //通过 fileoutputstream 获取对应的 filechannel
        //这个 fileChannel 真实的实现类型是 FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();

        //创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //将 str 放入 bytebuffer
        byteBuffer.put(str.getBytes());

        //对bytebuffer 进行flip
        byteBuffer.flip();

        // 将 bytebuffer 数据写入到 fileChannel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();

    }
}
